package com.xpec.c4.service.contexts;

/**
 * 生命周期定义
 * 
 * @author lipeilin
 * @2010-12-30 下午03:25:33
 *
 */
public enum ScopeType {
	
	   /**
	    * The event context<br>
	    * 每个消息请求是一个event过程
	    */
	   EVENT,
	   
	   /**
	    * The session context<br>
	    * 每个远端连接是一个session过程
	    */
	   SESSION,
	   
	   /**
	    * The application context<br>
	    * 整个程序是一个application过程
	    */
	   APPLICATION;
	   
//	   /**
//	    * The user context<br>
//	    * 每个玩家存在于服务器是一个user过程
//	    */
//	   USER;
	   
//	   /**
//	    * The group context<br>
//	    * 服务器组
//	    */
//	   GROUP;
	   
	   public boolean isContextActive()
	   {
	      switch (this)
	      {
	         case EVENT:
	            return Contexts.isEventContextActive();
	         case SESSION:
	            return Contexts.isSessionContextActive();
//	         case USER:
//	        	 return Contexts.isUserContextActive();
	         case APPLICATION:
	            return Contexts.isApplicationContextActive();
//	         case GROUP:
//	        	 return Contexts.isGroupContextActive();
	         default: 
	            throw new IllegalArgumentException();
	      }
	   }
	   
	   /**
	    * @return the Context object for this scope
	    */
	   public Context getContext() {
	      switch (this)
	      {
	         case EVENT: 
	            if ( !Contexts.isEventContextActive() )
	            {
	               throw new IllegalStateException("No event context active");
	            }
	            return Contexts.getEventContext();
	         case SESSION: 
	            if ( !Contexts.isSessionContextActive() )
	            {
	               throw new IllegalStateException("No session context active");
	            }
	             return Contexts.getSessionContext();
//	         case USER: 
//	        	 if ( !Contexts.isUserContextActive() )
//	        	 {
//	        		 throw new IllegalStateException("No user context active");
//	        	 }
//	        	 return Contexts.getUserContext();
	         case APPLICATION: 
	            if ( !Contexts.isApplicationContextActive() )
	            {
	               throw new IllegalStateException("No application context active");
	            }
	             return Contexts.getApplicationContext();
//	         case GROUP: 
//	        	 if ( !Contexts.isGroupContextActive() )
//	        	 {
//	        		 throw new IllegalStateException("No group context active");
//	        	 }
//	        	 return Contexts.getGroupContext();
	         default: 
	            throw new IllegalArgumentException();
	      }
	   }

}
